package org.hxyjs.dao.impl;

import org.hxyjs.dao.UserDao;
import org.hxyjs.entity.User;
import org.hxyjs.mapper.UserMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("userDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class UserDaoImpl implements UserDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private UserMapper userMapper;
    public List<User>  getAllUsers() throws Exception{
        return userMapper.selectByExample(null);
    }

    @Override
    public Integer deleteUserByUno(String uno)  throws Exception{
        return userMapper.deleteByPrimaryKey(uno);
    }

    @Override
    public Integer updateUserByUno(User user)  throws Exception{
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public Integer addUser(User user)  throws Exception{
        user.setUno(initId());
        return userMapper.insertSelective(user);
    }
    public String initId(){
        String maxUno = userMapper.getMaxUno();
        return GenNumTools.initId("v",4,maxUno);
    }
    @Override
    public List<User> getUserByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)userMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return userMapper.getUserByPage(map);
    }
    @Override
    public String getMaxUno(){
        return userMapper.getMaxUno();
    }
}
